﻿#include "ssTheodoliteCamera.h"


namespace StarSeeker
{

ssTheodoliteCamera::ssTheodoliteCamera(void)
{
	m_cameraType = CAMERA_TYPE_TL;
	m_posLookat.Set(0.0f, 0.0f, 0.0f);
	m_posUp.Set(0.0f, 0.0f, 1.0f);
	m_posEye.Set(0.0f, -35.0f, 35.0f);
	m_fFocus = 50.0f;
	m_fHorAngle = Math3D::gc_PI;
	m_fVerAngle = Math3D::gc_QUARTER_PI;
	m_fHorRadius = 0;
	m_transformDirty = false;
	//SetView();
}

ssTheodoliteCamera::~ssTheodoliteCamera(void)
{
}

void ssTheodoliteCamera::ChangeHorizontalAngle(ssfloat fVal)
{
	m_fHorAngle += fVal;
	if (m_fHorAngle>Math3D::gc_DOUBLE_PI)
		m_fHorAngle -= Math3D::gc_DOUBLE_PI;
	if (m_fHorAngle<0.0f)
		m_fHorAngle += Math3D::gc_DOUBLE_PI;
}

void ssTheodoliteCamera::ChangeVerticalAngle(ssfloat fVal)
{
	ssfloat fNewAngle = m_fVerAngle + fVal;
	if (fNewAngle>=Math3D::gc_HALF_PI || fNewAngle<=-Math3D::gc_HALF_PI)
		return;
	m_fVerAngle = fNewAngle;
}

void ssTheodoliteCamera::SetViewMatrix()
{
	LookAtRH(m_pmatView, &m_posEye, &m_posLookat, &m_posUp);
	m_transformDirty = false;
	//ComputeProjViewMatrix();
}

bool ssTheodoliteCamera::hasDirty()
{
	return m_transformDirty;
}

//计算视图矩阵
void ssTheodoliteCamera::LookAtRH(ssFLOAT4x4X *pOut, ssFLOAT3X *pEye, ssFLOAT3X *pAt, ssFLOAT3X *pUp)
{
	ssFLOAT3X zaxis = *pEye - *pAt;
	zaxis.Normalize();
	ssFLOAT3X xaxis = pUp->Cross(zaxis);
	xaxis.Normalize();
	ssFLOAT3X yaxis = zaxis.Cross(xaxis);
	yaxis.Normalize();
	pOut->_11=xaxis.x; pOut->_21=xaxis.y; pOut->_31=xaxis.z;
	pOut->_12=yaxis.x; pOut->_22=yaxis.y; pOut->_32=yaxis.z;
	pOut->_13=zaxis.x; pOut->_23=zaxis.y; pOut->_33=zaxis.z;
	pOut->_14=pOut->_24=pOut->_34=0.0f; pOut->_44=1.0f;
	pOut->_41 = -xaxis.Dot(*pEye);
	pOut->_42 = -yaxis.Dot(*pEye);
	pOut->_43 = -zaxis.Dot(*pEye);
}
//获取视口坐标,参数三维世界坐标
//bool CTheodoliteCamera::GetViewPortCoord(D3DXVECTOR3 *pIn, D3DXVECTOR2 *pOut)
//{
//	D3DXVECTOR3 c;
//	D3DXVec3TransformCoord(&c, pIn, &(m_matView*m_matProj));
//	pOut->x = m_fA*(1.f+c.x);
//	pOut->y = m_fB*(1.f-c.y);
//	//if((u32)pOut->x<=m_ViewPort.Width && (u32)pOut->y<=m_ViewPort.Height)
//	//	return true;
//	//else
//	return false;
//}

}
